package com.supreme.orderserver.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supreme.common.entityVO.CreateOrderVO;
import com.supreme.orderserver.datamapper.OrderDtoMapper;
import com.supreme.common.dto.CompleteOrder;
import com.supreme.orderserver.service.OrderService;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonentity.mysql.primary.BizOrder;
import com.supreme.commonmybatis.mapper.OrderMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "order")
public class OrderController {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderService orderService;

    @Resource
    private OrderDtoMapper orderDtoMapper;

    @GetMapping(value = "orders")
    public AjaxResp getOrderList() {
        final List<BizOrder> orderList = orderMapper.selectList(Wrappers.emptyWrapper());
        return AjaxResp.instance(orderList);
    }

    @GetMapping(value = "orders/{orderId}")
    public AjaxResp getOrder(@PathVariable(value = "orderId") Long orderId) {
        final BizOrder order = orderMapper.selectById(orderId);
        return AjaxResp.instance(order);
    }

    @GetMapping(value = "mapper.orders/{orderId}")
    public AjaxResp getMapperOrder(@PathVariable(value = "orderId") Long orderId) {
        final BizOrder order = orderMapper.selectById(orderId);
        final CompleteOrder completeOrder = orderDtoMapper.mappingToCompleteOrder(order);
        return AjaxResp.instance(completeOrder);
    }

    @PostMapping(value = "orders")
    public AjaxResp createOrder(@RequestBody CreateOrderVO createOrderVO) {
        final int result = orderService.createOrder(createOrderVO);
        return AjaxResp.instance(result);
    }
}
